/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#include "ScrollBar.h"
#include "../ScrollBar.h"
#include <lib2d/hard/Renderer.h>

namespace liba
{
namespace lib2d
{
namespace gui
{
namespace view
{

ScrollBar::ScrollBar( gui::ScrollBar * obj )
:	Object( obj ),
	obj( obj )
{}

bool ScrollBar::on_attribute( xml::Provider * prov, const std::string & name, const std::string & value )
{
	if( name == "Image" )
	{
		image = new objects::ImageSimple( value );
		return true;
	}
	if( name == "Track" )
		return converter::convert( value, &track_rect );
	if( name == "Back" )
		return converter::convert( value, &back_rect );
	return false;
}

bool ScrollBar::on_node( xml::Provider * prov, const std::string & name )
{
	if( name == "Center" )
		return prov->parse_object( obj->center->get_view() );
	if( name == "Top" )
		return prov->parse_object( obj->top->get_view() );
	if( name == "Bottom" )
		return prov->parse_object( obj->bottom->get_view() );

	return false;
}

void ScrollBar::render( hard::Renderer * rend )
{
	image->fill_rect( rend, Rect( 0, 0, obj->get_window_rect().width, obj->get_window_rect().height ), back_rect );
	Object::render( rend );
	Vector2 pos( 0, obj->center->get_window_rect().y + obj->center->get_window_rect().height/2 - track_rect.height/2 );
	image->blit( rend, pos, track_rect );
}

} //namespace view
} //namespace gui
} //namespace lib2d
} //namespace liba

